//
// Created by wujehy on 2020/9/13.
//

#include <base_types.h>
#include "ClientCmdFunction.h"
#include "ClientService.h"
#include <glog/logging.h>
#include <iostream>

#define CallFunctionINPUTString(NAME)  std::string NAME; \
                        std::cout << " input "<< #NAME<<":" <<std::flush; \
                        std::cin >> NAME;
using namespace GeeJoan;
int64_t cache_startTime = 0;

int64_t getLastTime();

//　实现
static int64_t getTimeMicro()
{
    std::chrono::time_point<std::chrono::system_clock> p = std::chrono::system_clock::now();
    return std::chrono::duration_cast<std::chrono::microseconds>(p.time_since_epoch()).count();
}

void Test1Function(ClientService *client)
{
    //发送状态回调
    auto callbackStatus = [](int status)
    {
        LOGINFO << "semd callback status = " << status;
    };
//    LOGINFO<<" sent time  = " << getTimeMicro();
    client->send(ModType_TestMod, OpTestModSubType_Test1, "test1", callbackStatus);
}

//1600051907369095
//1600051907378528
void Test2Function(GeeJoan::ClientService *client)
{
    auto callbackStatus = [](int status)
    {
        LOGINFO << "semd callback status = " << status;
    };
    cache_startTime = getTimeMicro();
    std::cout << " send start  = " << cache_startTime;

    for (int i = 0; i < 10000; i++)
    {
        client->send(ModType_TestMod, OpTestModSubType_Test1, "test1", callbackStatus);
    }
    client->send(ModType_TestMod, OpTestModSubType_Test2, "end", callbackStatus);
}

void Test3Function(GeeJoan::ClientService *client)
{
    client->send(ModType_TestMod, OpTestModSubType_Test3, "end");

}

void ShowStartTimeFunction(GeeJoan::ClientService *client)
{
    int64_t useTime = getLastTime() - cache_startTime;
    std::cout << " start Time = " << cache_startTime << " last time = " << getLastTime() << std::endl;
    std::cout << " useTime = " << useTime << std::endl;
}

//void InitLoggingTimeFunction(GeeJoan::ClientService *client)
//{
//
//}

void CloseLoggingFunction(GeeJoan::ClientService *client)
{
    google::SetStderrLogging(google::GLOG_WARNING);

}

void ShowLoggingFunction(GeeJoan::ClientService *client)
{
    google::SetStderrLogging(google::GLOG_INFO);

}

void SetKeyValueFunction(GeeJoan::ClientService *client)
{
    CallFunctionINPUTString(key);
    CallFunctionINPUTString(value);

    // 序列化 与服务端对应解包

    std::string messageBuff;
    messageBuff.push_back(key.length());
    messageBuff.append(key);
    messageBuff.push_back(value.length());
    messageBuff.append(value);
    auto callbackStatus = [](int status)
    {
        LOGINFO << "semd callback status = " << status;
    };
    client->send(ModType_KeyValueMod, OpKeyValueModSubType_set, messageBuff, callbackStatus);


}

void GetKeyValueFunction(GeeJoan::ClientService *client)
{

    CallFunctionINPUTString(get_key);


    auto callbackStatus = [](int status)
    {
        LOGINFO << "semd callback status = " << status;
    };
    client->send(ModType_KeyValueMod, OpKeyValueModSubType_get, get_key, callbackStatus);


}
